package com.mb.user.controller;

import com.mb.common.pojo.User;
import com.mb.common.utils.CookieUtils;
import com.mb.common.vo.SysResult;
import com.mb.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user/manage")
public class UserController {
    @Autowired
    private UserService userService;
    /*
    * 1.用户名重复的校验
    * */
    @RequestMapping("/checkUserName")
    public SysResult checkName(String username){
        boolean name = userService.checkUsername(username);
        if(name){
            return SysResult.ok();
        }else {
            return SysResult.build(201,"用户名已存在",null);
        }
    }
    /*
    * 2.注册用户的实现
    * */
    @RequestMapping("/save")
    public SysResult doRegist(User user){
        try{
            userService.doRegist(user);
            return SysResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return SysResult.build(201,"注册用户失败",null);
        }
    }
    /**
     * 登录校验
     */
    @RequestMapping("/login")
    public SysResult doLogin(User user, HttpServletResponse response, HttpServletRequest request){
        String ticket = userService.doLogin(user);
        if(StringUtils.isEmpty(ticket)){
            return SysResult.build(201,"登录失败",null);
        }else {
            //成功  发送客户端
            CookieUtils.setCookie(request,response,
                    "EM_TICKET",ticket);
            return SysResult.ok();
        }
    }
    /*
    * 登录状态
    * */
    @RequestMapping("/query/{ticket}")
    public SysResult queryUserJson(@PathVariable String ticket){
        String userJson = userService.queryUserJson(ticket);
        //userjson可能为空 超时就拿不到该数据
        if(StringUtils.isEmpty(userJson)) {
            return SysResult.build(201, "登录超时", null);
        }else{
            return SysResult.build(200,"获取登录信息",userJson);
        }
    }
    /*
     * 登出功能实现
     * */
    @RequestMapping("/logout")
    public SysResult doLogout(HttpServletRequest request,HttpServletResponse response){
        try{
            Cookie[] cookies = request.getCookies();
            Cookie emticket = null;
            String ticket = "";
            for(Cookie cookie : cookies){
                if("EM_TICKET".equals(cookie.getName())){
                    emticket=cookie;
                    ticket=cookie.getValue();
                }
            }
            if(emticket!=null){
                emticket.setMaxAge(0);
                emticket.setPath("/");
            }
            response.addCookie(emticket);
            //删除redis
            userService.deleteTicket(ticket);
            return SysResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return SysResult.build(201,"登出失败",null);
        }
    }
}
